﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics.Contracts;
using ImapX.Parsing.Tokens;

namespace ImapX.Parsing.DataItems
{
    public class Flags : DataItem, IEnumerable<string>
    {
        NList<UnquotedString> _flagList = new NList<UnquotedString>();

        public Flags(bool canBeNIL = false)
            : base(canBeNIL)
        {
            SetParts(_flagList);
        }

        public string this[int index]
        {
            get
            {
                return _flagList[index].Value;
            }
        }

        public void Add(string flag)
        {
            _flagList.Add(new UnquotedString() { Value = flag });
        }

        public void Clear()
        {
            _flagList.Clear();
        }

        private void SetFlags(string flags)
        {
            string[] strArray = flags.Split(' ');
            _flagList.Clear();
            foreach (string s in strArray) _flagList.Add(new UnquotedString() { Value = s });
        }

        override public string Command { get { return "FLAGS"; } }

        public override string ToString()
        {
            return String.Join(" ", this);
        }

        protected override string BeginPattern
        {
            get
            {
                return Command + @"\s";
            }
        }

        public IEnumerator<string> GetEnumerator()
        {
            foreach (NString s in _flagList) 
            {
                yield return s.Value;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _flagList.GetEnumerator();
        }

    }
}
